<?php
/**
 * @author 		supfire
 * @copyright		yctool.com
 * @version 		1.0
 * @name 		msg.func.php
 * @todo 		Define the msg function
 * @final 		2009-05-18
 */
!defined("IN_YCTOOL") && exit();

/**
 * 有新消息 输出php文件纪录
 *
 * @param int $type           消息类型
 *				    1 为 短消息
 *				    2 为 系统消息
 *				    3 为 评论
 *				    4 为 回复评论
 *
 *				   11 为 礼物
 *				   12 为 动Ta一下
 *				    等。
 * @param str $userid         用户ID
 * @param str $userpathfile   文件路径+文件名称，不必加后缀 .php
 * @param str bool		  
 */
function writeNewNote($touid, $type = 0, $userpathpre = '')
{

    $touid    = substr($touid, 0, -1);
    $touid = array_unique(explode(",", $touid));

    if(!$touid){ return false;}

    foreach ($touid as $j => $userid)
    {
	$userpathfilepre = $userpathpre.'userdata/'.Func::getFilePath($userid, 'config');
	if (file_exists($userpathfilepre))
	{
	    $newmsg		= 0;
	    $sysmsg		= 0;
	    $commentmsg	= 0;
	    $recommentmsg	= 0;
	    $actmsg		= 0;
	    $giftmsg	= 0;

	    $userpathfile = $userpathfilepre .'newnote';
	    if (file_exists($userpathfile.'.php'))
	    {
		require_once($userpathfile.'.php');
		$newmsg		= $variablenewnote[$userid]['newmsg'];
		$sysmsg		= $variablenewnote[$userid]['sysmsg'];
		$commentmsg	= $variablenewnote[$userid]['commentmsg'];
		$recommentmsg	= $variablenewnote[$userid]['recommentmsg'];
		$actmsg		= $variablenewnote[$userid]['actmsg'];
		$giftmsg	= $variablenewnote[$userid]['giftmsg'];
	    }

	    switch ($type)
	    {
		case 1:
		    $newmsg		= $newmsg + 1;
		    break;
		case 2:
		    $sysmsg		= $sysmsg + 1;
		    break;
		case 3:
		    $commentmsg	= $commentmsg + 1;
		    break;
		case 4:
		    $recommentmsg	= $recommentmsg + 1;
		    break;
    	    case 11:
		    $giftmsg	= $giftmsg + 1;
		    break;
		case 12:
		    $actmsg		= $actmsg + 1;
		    break;
	    }

	    $arr = array();
	    $arr[$userid]["newmsg"]		    = $newmsg;
	    $arr[$userid]["sysmsg"]		    = $sysmsg;
	    $arr[$userid]["commentmsg"]		    = $commentmsg;
	    $arr[$userid]["recommentmsg"]	    = $recommentmsg;
	    $arr[$userid]["actmsg"]		    = $actmsg;
	    $arr[$userid]["giftmsg"]		    = $giftmsg;

	    //print_r($arr);
	    //exit;
	    func::operateFile('variablenewnote', $arr, $userpathfile, 'php', 2);

	    $arr = array();
	    $arr[$userid]["newmsg"] = 1;
	    func::operateFile('variablenewnotemsg', $arr, $userpathfile.'msg', 'php', 2);
	}
    }
}

/**
 * 读新消息 php文件纪录该类 消息数目为 0
 *
 * @param int $type           消息类型
 *				    1 为 短消息
 *				    2 为 系统消息
 *				    3 为 评论
 *				    4 为 回复评论
 *
 *				   11 为 礼物
 *				   12 为 动Ta一下
 *				    等。
 * @param str $userid         用户ID
 * @param str $userpathfile   文件路径+文件名称，不必加后缀 .php
 * @param str bool		  
 */
function readNewNote($userid, $type = 0)
{
    $userpathfile = 'userdata/'.Func::getFilePath($userid, 'config').'newnote';
    if (file_exists($userpathfile.'.php'))
    {
	require_once($userpathfile.'.php');

	$notecount = $variablenewnote[$userid]['newmsg'] +
	$variablenewnote[$userid]['sysmsg'] + 
	$variablenewnote[$userid]['commentmsg'] + 
	$variablenewnote[$userid]['recommentmsg'] +
	$variablenewnote[$userid]['actmsg'] +
	$variablenewnote[$userid]['giftmsg'];

	switch ($type)
	{
	    case 1:
		$variablenewnote[$userid]['newmsg']		= 0;
		break;
	    case 2:
		$variablenewnote[$userid]['sysmsg']		= 0;
		break;
	    case 3:
		$variablenewnote[$userid]['commentmsg']		= 0;
		break;
	    case 4:
		$variablenewnote[$userid]['recommentmsg']	= 0;
		break;
	    case 11:
		$variablenewnote[$userid]['giftmsg']		= 0;
		break;
	    case 12:
		$variablenewnote[$userid]['actmsg']		= 0;
		break;
	}
    }
    if($notecount)
    {
	func::operateFile('variablenewnote', $variablenewnote, $userpathfile, 'php', 2);
    }
}

/**
 * 读新消息 php文件纪录该类 消息数目为 0
 *
 * @param int $type           消息类型
 *				    1 为 短消息
 *				    2 为 系统消息
 *				    3 为 评论
 *				    4 为 回复评论
 *				    等。
 * @param str $userid         用户ID
 * @param str $userpathfile   文件路径+文件名称，不必加后缀 .php
 * @param str bool		  
 */
function readNewNoteMsg($userid)
{
    $userpathfile = 'userdata/'.Func::getFilePath($userid, 'config').'newnotemsg';
    if (file_exists($userpathfile.'.php'))
    {
	require_once($userpathfile.'.php');
	if($variablenewnotemsg[$userid]['newmsg'])
	{
	    $arr = array();
	    $arr[$userid]["newmsg"]		    = 0;
	    func::operateFile('variablenewnotemsg', $arr, $userpathfile, 'php', 2);
	}
    }
}

/**
 * 探测有无新消息 [建议： 3分钟探测一次 ] 
 *
 * @param str $userid         用户ID
 * @param str $userpathfile   文件路径+文件名称，不必加后缀 .php
 * @param str bool		  
 */
function haveNewNote($userid)
{
    $userpathfile = 'userdata/'.Func::getFilePath($userid, 'config').'newnotemsg';
    if (file_exists($userpathfile.'.php'))
    {
	require_once($userpathfile.'.php');
	return $variablenewnotemsg[$userid]['newmsg'];
    }
    else
    {
	return 0;
    }
}

    /**
     * 输出动态操作
     *
     * @param int $type           动态类型
     *				    1 为加为好友
     *				    2 为修改心情状态
     *				    3 修改头像
     *				    4 修改资料
     *				    5 我的照片
     *				    6 日志
     *
     *				    7 捐赠
     *				    8 共享
     *				    9 活动
     *
     *				   11 礼物
     *				   12 动他一下
     *				    等。
     * @param str $user           用户串
     * @param str $friend         被动作的用户串
     * @param str $message        HTML显示动态
     */
    function msgAction($type, $user, $friend, $addtime, $message, $action = 'guest')
    {
	global $userdatapath;
	global $variableact;
	global $variableactF;
	global $variablegift;
	global $Upath;

	$type == 1  && $str = '<img src="image/default/msg'.$type.'.gif" />&nbsp;';


	switch ($type)
	{
	    case 1:
		$str .= $user.' 与 '.$friend.' 成为好友 ';
		break;
	    case 2:
		$str .= ' 修改了签名为 '. $message ;
		break;
	    case 3:
		$str .= ' 修改了头像为 '. $message ;
		break;
	    case 4:
		$str .= ' 修改了 '. $message ;
		break;
	    case 5:
		$str .= ' 添加了照片 '. $message ;		
		break;
	    case 6:
		$str .= ' 添加了日志 '. $message ;		
		break;
	    case 7:
		$str .= ' 添加了捐赠 '. $message ;		
		break;
	    case 8:
		$str .= ' 添加了共享 '. $message ;		
		break;
	    case 9:
		$str .= ' 添加了活动 '. $message ;		
		break;
	    case 11:
		switch ($message)
		{
		case 1:
			$gift1 = '玫瑰';
			$img = 'userdata/face/11.gif';
		    break;
		case 2:
			$gift1 = '名贵车';
			$img = 'userdata/face/12.gif';
		    break;
		case 3:
			$gift1 = '美酒';
			$img = 'userdata/face/13.gif';
		    break;
		case 4:
			$gift1 = '钻戒';
			$img = 'userdata/face/14.gif';
		    break;
		case 5:
			$gift1 = '内衣';
			$img = 'userdata/face/15.gif';
		    break;
		case 6:
			$gift1 = '蛋糕';
			$img = 'userdata/face/16.gif';
		    break;

		default:
			@include_once($Upath . 'gift/gift.php');
			    if(file_exists($Upath.'gift/'.$message.'0.jpg'))
			    {
				$img = $Upath.'gift/'.$message.'.jpg';
			    }
			    else
			    {
				$img = 'image/default/sysgift.gif';
			    }
			    $gift1 = $variablegift[$message]['gift1'];
		}
		$str .= $user.'送给'.$friend . '礼物 <div style="float:left;"><IMG src="' . $img . '" alt="'. $gift1 .'"></div>';
		break;

	    case 12:
		switch ($message)
		{
		case 1:
			$act1 = '动了';
			$act2 = '一下';
			$img = 'userdata/face/1.gif';
		    break;
		case 2:
			$act1 = '电了';
			$act2 = '一下';
			$img = 'userdata/face/2.gif';
		    break;
		case 3:
			$act1 = '亲了';
			$act2 = '一下';
			$img = 'userdata/face/3.gif';
		    break;
		case 4:
			$act1 = '对';
			$act2 = '踩~ ~ 踩~ ~ 踩~ ~ ';
			$img = 'userdata/face/4.gif';
		    break;
		case 5:
			$act1 = '捅了';
			$act2 = '一下';
			$img = 'userdata/face/5.gif';
		    break;
		default:
			@include_once($Upath . 'act/act.php');
			if($variableact[$message])
			{
			    if (file_exists($Upath.'act/'.$message.'0.jpg'))
			    {
				$img = $Upath.'act/'.$message.'.jpg';
			    }
			    else
			    {
				$img = 'image/default/sysact.gif';
			    }
			    $act1 = $variableact[$message]['act1'];
			    $act2 = $variableact[$message]['act2'];
			}
			else
			{
			    @include_once($userdatapath . 'act/act.php');
			    if(file_exists($userdatapath.'act/'.$message.'0.jpg'))
			    {
				$img = $userdatapath.'act/'.$message.'.jpg';
			    }
			    else
			    {
				$img = 'image/default/sysact.gif';
			    }
			    $act1 = $variableact[$message]['act1'];
			    $act2 = $variableact[$message]['act2'];
			}
		    if(!$act1 && !$act2) { $act1 = '动了';  $act2 = '一下'; }
		}
		$str .= $user.$act1.$friend.$act2 . '<div style="float:left;"><IMG src="' . $img . '"></div>';
		break;

	    case 0:
                $str .= $message;
	break;
            default:;
		
	}

	return $str .= '';
    }

    /**
     * 输出系统消息
     *
     * @param int $type           系统消息类型
     *				    1 加为好友申请 
     *				    X
     *				    3 评论
     *				    4 评论回复
     *				    5 照片
     *				    6 日志
     *				    7 收藏
     *
     *				    11 送他礼物
     *				    12 动他一下
     *				    
     *
     * @param int $isreply	  isreply 1 同意， 2 忽略			   
     *				    等。
     * @param str $user           用户串 
     * @param str $friend         被动作的用户串
     * @param str $message        HTML显示动态
     */
    function sysMsgAction($type, $userid, $friendid, $addtime, $message, $isreply = 0, $sysid = 0, $username = '')
    {
	global $useridpath;
	global $variableact;
	global $variableactF;
	global $variablegift;
	global $arr_all_tomyfriend;
	$message = stripslashes($message);
	$message = str_replace(' ', '&nbsp;', $message);
	$message = str_replace('<a&nbsp;href=', '<a href=', $message);
	$message = str_replace('<img&nbsp;src', '<img src', $message);

	$message = str_replace("\n", '<br />', $message);
	$userdatapath = 'userdata/' . Func::getFilePath($friendid);
	$str = '
	<div>
	';
	switch ($type)
	{
	    case 1:
	    $str .= '
			<b class="links_greenb"><a href="user.php?uid='. $friendid .'">'. $username .'</a></b>
				请求加您为好友 </div>
			<h4 ><a href="msg.php?action=tome&del=1&act=to&msgid='.$sysid.'"><img src="common/img/del.gif" /></a></h4>
			<table style="table-layout:fixed"><tr><td><div style="margin-top:10px;margin-bottom:10px;word-break: break-all;overflow:hidden;">
			    '.$message.'';
		if(!$isreply)
		{
		    $str .= '
			</div>
			<div>
			    <a href="msg.php?action=tome&act=isreply&friendid='.$friendid.'&isreply=1&msgid='.$sysid.'"> 同意 </a>
			     ┊  
			    <a href="msg.php?action=tome&act=isreply&friendid='.$friendid.'&isreply=2&msgid='.$sysid.'"> 忽略 </a>
			    ';
		}
		$str .= '
			    </div></td></tr></table>
			    
			    ';
		if($isreply == 1)
		{
		    $str .= '<h5 class="links_graylight">已通过了对方请求</h5>';
		}elseif($isreply == 2)
		{
		    $str .= '<h5 class="links_graylight">已忽略了对方请求</h5>';
		}
		break;
	    case 3:
		 $str .= '收到评论</b></div>
				<div style="float:left;text-align:center;margin:5px;">
				<p class=l50_s>
				<a href="user.php?uid='.$friendid.'"
				><IMG height=50 src="'. $userdatapath .'config/face0.jpg" width=50 border="0"></a> 
				</p>
				<a href="user.php?uid='. $friendid .'"
				>'. $arr_all_tomyfriend[$friendid]['username'] .'</a>
				</div>

				<table style="table-layout:fixed"><tr><td><div style="margin:10px;word-break: break-all;overflow:hidden;">
				'.$message.'';
		$str .= '
			    </div></td></tr></table>
			    </li>
			    <li>';
		break;
	    case 4:
		 $str .= '收到回复</b></div>
				<div style="float:left;text-align:center;margin:5px;">
				<p class=l50_s>
				<a href="user.php?uid='.$friendid.'"
				><IMG height=50 src="'. $userdatapath .'config/face0.jpg" width=50 border="0"></a> 
				</p>
				<a href="user.php?uid='. $friendid .'"
				>'. $arr_all_tomyfriend[$friendid]['username'] .'</a>
				</div>

				<table style="table-layout:fixed"><tr><td><div style="margin-top:10px;margin-bottom:10px;word-break: break-all;overflow:hidden;">
				'.$message.'';
		$str .= '
			    </div></td></tr></table>
			    </li>
			    <li>';
		break;


	 case 11:
		switch ($message)
		{
		case 1:
			$gift1 = '玫瑰';
			$img = 'userdata/face/11.gif';
		    break;
		case 2:
			$gift1 = '名贵车';
			$img = 'userdata/face/12.gif';
		    break;
		case 3:
			$gift1 = '美酒';
			$img = 'userdata/face/13.gif';
		    break;
		case 4:
			$gift1 = '钻戒';
			$img = 'userdata/face/14.gif';
		    break;
		case 5:
			$gift1 = '内衣';
			$img = 'userdata/face/15.gif';
		    break;
		case 6:
			$gift1 = '蛋糕';
			$img = 'userdata/face/16.gif';
		    break;
		default:
			/*if($variablegiftF[$message])
			{
			    if (file_exists($useridpath.'gift/'.$message.'0.jpg'))
			    {
				$img = $useridpath.'gift/'.$message.'.jpg';
			    }
			    else
			    {
				$img = 'image/default/sysgift.gif';
			    }
			    $gift1 = $variablegiftF[$message]['gift1'];
			}
			else
			{*/
			    @include_once($userdatapath . 'gift/gift.php');
			    if(file_exists($userdatapath.'gift/'.$message.'0.jpg'))
			    {
				$img = $userdatapath.'gift/'.$message.'.jpg';
			    }
			    else
			    {
				$img = 'image/default/sysgift.gif';
			    }
			    $gift1 = $variablegift[$message]['gift1'];
			/*}*/
		}
		$str .= '收到礼物</b></div>
				<div style="float:left;text-align:center;margin:5px;">
				<p class=l50_s>
				<a href="user.php?uid='.$friendid.'"
				><IMG height=50 src="'. $userdatapath .'config/face0.jpg" width=50></a> 
				</p>
				</div>
				<table style="table-layout:fixed"><tr><td><div style="margin-left:30px;word-break: break-all;overflow:hidden;">
				<a href="user.php?uid='. $friendid .'"
				>'. $arr_all_tomyfriend[$friendid]['username'] .'</a>送来礼物
				<br />
				<img src="'. $img .'" alt="'. $gift1 .'" style="margin:10px;">
				';
		if(!$isreply)
		{
		    $str .= '
			    <br />
<a href="msg.php?action=gift&act=isreply&friendid='.$friendid.'&sysid='.$sysid.'&isreply=2">回赠礼物</a>
			    ';
		}
		elseif($isreply == 1 || $isreply == 2)
		{
		    $str .= '<br /><a href="plugins/gift/index.php?uid='.$friendid.'">再给Ta一个礼物</a>';
		
		}
		$str .= '
			    </div></td></tr></table>
			    </li>
			    <li>';
		if($isreply == 2)
		{
		    $str .= '<div style="background-color:#FFCC66; padding:2px;border: dashed #FF9900 1px;">你已经回赠礼物</div>';
		}
		break;


	    case 12:
		switch ($message)
		{
		case 1:
			$act1 = '动';
			$act2 = '一下';
			$img = 'userdata/face/1.gif';
		    break;
		case 2:
			$act1 = '电';
			$act2 = '一下';
			$img = 'userdata/face/2.gif';
		    break;
		case 3:
			$act1 = '亲';
			$act2 = '一下';
			$img = 'userdata/face/3.gif';
		    break;
		case 4:
			$act1 = '对';
			$act2 = '踩~ ~ 踩~ ~ 踩~ ~ ';
			$img = 'userdata/face/4.gif';
		    break;
		case 5:
			$act1 = '捅';
			$act2 = '一下';
			$img = 'userdata/face/5.gif';
		    break;
		default:
			if($variableactF[$message])
			{
			    if (file_exists($useridpath.'act/'.$message.'0.jpg'))
			    {
				$img = $useridpath.'act/'.$message.'.jpg';
			    }
			    else
			    {
				$img = 'image/default/sysact.gif';
			    }
			    $act1 = $variableactF[$message]['act1'];
			    $act2 = $variableactF[$message]['act2'];
			}
			else
			{
			    @include_once($userdatapath . 'act/act.php');
			    if(file_exists($userdatapath.'act/'.$message.'0.jpg'))
			    {
				$img = $userdatapath.'act/'.$message.'.jpg';
			    }
			    else
			    {
				$img = 'image/default/sysact.gif';
			    }
			    $act1 = $variableact[$message]['act1'];
			    $act2 = $variableact[$message]['act2'];
			}
		}
		$str .= '动我一下</b></div>
				<div style="float:left;text-align:center;margin:5px;">
				<p class=l50_s>
				<a href="user.php?uid='.$friendid.'"
				><IMG height=50 src="'. $userdatapath .'config/face0.jpg" width=50></a> 
				</p>
				</div>
				<table style="table-layout:fixed"><tr><td><div style="margin-left:30px;word-break: break-all;overflow:hidden;">
				<a href="user.php?uid='. $friendid .'"
				>'. $arr_all_tomyfriend[$friendid]['username'] .'</a>'.$act1.'你'.$act2.'
				<br />
				<img src="'. $img .'" style="margin:10px;">
				';
		if(!$isreply)
		{
		    $str .= '
			    <br />
<a href="msg.php?action=act&act=isreply&actid='.$message.'&friendid='.$friendid.'&sysid='.$sysid.'&isreply=1">也'.$act1.'Ta'.$act2.'</a>
┊  
<a href="msg.php?action=act&act=isreply&actid='.$message.'&friendid='.$friendid.'&sysid='.$sysid.'&isreply=2">用其它动作回应Ta</a>
			    ';
		}
		else
		{
		    $str .= '<br /><a href="plugins/act/index.php?uid='.$friendid.'">再给Ta一个动作</a>';
		
		}
		$str .= '
			    </div></td></tr></table>
			    </li>
			    <li>';
		if($isreply == 1)
		{
		    $str .= '<div style="background-color:#FFCC66; padding:2px;border: dashed #FF9900 1px;">你已经用同样的动作回复Ta</div>';
		}elseif($isreply == 2)
		{
		    $str .= '<div style="background-color:#FFCC66; padding:2px;border: dashed #FF9900 1px;">你已经用其它的动作回复Ta</div>';
		}
		break;

	   default:
		$str .= $user.' : '. $friendid ;
	}
	return $str;
    }
?>
